let axios = (function () {
    let  BaseUrl = 'http://localhost:8888/api'
    function ajax_get({ url, query }) {
        return new Promise((resolve, reject) => {
            if (!url) {
                return alert('你没有传递url')
            }
            let str = ''
            if (query) {
                str = '?'
                for (let key in query) {
                    str += `${key}=${query[key]}&`
                }
                str = str.slice(0, -1)
            }

            let xhr = new XMLHttpRequest()
            xhr.open('get', query ? BaseUrl+url + str : BaseUrl+url)
             // 设置请求头 携带token
            xhr.setRequestHeader('authorization',cookie.getItem('token') || null)
            xhr.send()
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        resolve(JSON.parse(xhr.response))
                    } else {
                        reject(JSON.parse(xhr.response))
                    }
                }
            }
        })

    }


    function ajax_post({ url, query }) {
        return new Promise((resolve, reject) => {
            if (!url) {
                return alert('你没有传递地址')
            }
            // let str = ''
            // if (query) {
            //     for (let key in query) {
            //         str += `${key}=${query[key]}&`
            //     }
            //     str = str.slice(0, -1)
            // }

            let xhr = new XMLHttpRequest()
            xhr.open('post', BaseUrl+url)
            xhr.setRequestHeader('authorization',cookie.getItem('token') || null)
            // xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
            xhr.setRequestHeader('content-type','application/json')
            xhr.send(query ?JSON.stringify(query) : null)
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        resolve(JSON.parse(xhr.response))
                    } else {
                        reject(JSON.parse(xhr.response))
                    }
                }
            }
        })
    }

    
    function ajax_put({ url, query }) {
        return new Promise((resolve, reject) => {
            if (!url) {
                return alert('你没有传递地址')
            }
            // let str = ''
            // if (query) {
            //     for (let key in query) {
            //         str += `${key}=${query[key]}&`
            //     }
            //     str = str.slice(0, -1)
            // }

            let xhr = new XMLHttpRequest()
            xhr.open('put', BaseUrl+url)
            xhr.setRequestHeader('authorization',cookie.getItem('token') || null)
            // xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
            xhr.setRequestHeader('content-type','application/json')
            xhr.send(query ?JSON.stringify(query) : null)
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        resolve(JSON.parse(xhr.response))
                    } else {
                        reject(JSON.parse(xhr.response))
                    }
                }
            }
        })
    }


    function ajax_upload({ url, query }) {
        return new Promise((resolve, reject) => {
            if (!url) {
                return alert('你没有传递地址')
            }
            let xhr = new XMLHttpRequest()
            xhr.open('post', BaseUrl+url)
            xhr.setRequestHeader('authorization',cookie.getItem('token') || null)
            xhr.send(query)
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        resolve(JSON.parse(xhr.response))
                    } else {
                        reject(JSON.parse(xhr.response))
                    }
                }
            }
        })
    }
    let options = {
        get: ajax_get,
        post: ajax_post,
        put:ajax_put,
        upload:ajax_upload
    }
    return options
})()


const cookie = (function () {

    /* 
          @params {String} key cookie的名称 
          @params {String} value cookie的值 
          @params {Number} time cookie的过期时间 秒  
        
        */
    function setItem(key, value, time) {
        let expires = null

        // 判断传过来time没有
        if (time) {
            let d = new Date()
            let times = d.getTime() + time * 1000
            d.setTime(times)
            expires = d.toUTCString()
        }
        document.cookie = `${key}=${value};expires=` + expires
    }

    // 获取单个cookie的值
    function getItem(key) {
        let obj = {}
        let arr = document.cookie.split('; ')
        arr.forEach(it => {
            let res = it.split('=')
            obj[res[0]] = res[1]
        })
        return obj[key]
    }


    // 删除单个cookie
    function removeItem(key) {
        setItem(key, 0, -1)
    }
    // 清除所有的cookie
    function clear() {
        let obj = {}
        let arr = document.cookie.split('; ')
        arr.forEach(it => {
            let res = it.split('=')
            removeItem(res[0])
        })
    }


    let cookie = {
        setItem,
        getItem,
        removeItem,
        clear
    }

    return cookie
})()

// 获取查询字符串的参数
function getParams(key) {
    let obj = {}
    let arr = location.search.slice(1).split('&')
    arr.forEach(it => {
        let res = it.split('=')
        obj[res[0]] = res[1]
    })
    console.log(obj)
    return obj[key]
}